package top.wardyan.wdev.dbdevassist.action.dic;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableItem;

import top.wardyan.base.util.DateUtil;
import top.wardyan.conf.Const;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WTable;
import top.wardyan.control.WTableColumn;
import top.wardyan.control.WTableItem;
import top.wardyan.control.WText;
import top.wardyan.wdev.dbdevassist.action.base.BaseShell;
import top.wardyan.wdev.dbdevassist.action.base.WDialog;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.YTDic;
import top.wardyan.wdev.dbdevassist.model.YTDicItem;

public class DicShell2 extends BaseShell<DicShell2>{
	protected WComposite mainCom;
	protected WComposite colCom;
	protected WTable mainTable;
	protected WTable colTable;
	protected WLabel mainInfo;
	protected WText mainSearchText;
	protected WButton btnMainAdd;
	protected WButton btnDetailAdd;
	
	public DicShell2() {
		super();
		this.createContent();
		this.setMaximized(true);
		this.resize();
	}
	
	private void resize() {
		mainCom.setBounds(10, topCom.getVertexY() + 10, (this.getSize().x - 30) / 2, this.getSize().y - topCom.getVertexY() - 10);
		mainSearchText.setBounds(0, 0, mainCom.getSize().x - 70, 30);
		btnMainAdd.setBounds(mainSearchText.getVertexX() + 10, mainSearchText.getLocation().y, 60, mainSearchText.getSize().y);
		//mainTable.setBounds(0, 0, mainCom.getSize().x - 10, mainCom.getSize().y - 30);
		mainTable.setBounds(0, mainSearchText.getVertexY() + 10, mainCom.getSize().x - 10, mainCom.getSize().y - mainSearchText.getVertexY() - 40);
		
		colCom.setBounds(mainCom.getVertexX() + 10, mainCom.getLocation().y, mainCom.getSize().x, mainCom.getSize().y);
		btnDetailAdd.setBounds(colCom.getSize().x - 70, 0, 60, 30);
		colTable.setBounds(0, btnDetailAdd.getVertexY() + 10, colCom.getSize().x - 10, colCom.getSize().y - btnDetailAdd.getVertexY() - 30);
	}
	
	private void createContent() {
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				resize();
			}
		});
		
		mainCom = new WComposite(this, SWT.NONE, 10, topCom.getVertexY() + 10, (this.getSize().x - 30) / 2, this.getSize().y - topCom.getVertexY() - 10, Const.COLOR_WHITE);
		
		mainSearchText = new WText(mainCom, 0, 0, mainCom.getSize().x - 70, 30, "", "");
		mainSearchText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				self.listMain(true);
			}
		});
		mainSearchText.setFocus();
		btnMainAdd = new WButton(mainCom, mainSearchText.getVertexX() + 10, mainSearchText.getLocation().y, 60, mainSearchText.getSize().y, "新增", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new WDialog(self, new DicAddShell()).open();
				listMain(true);
			}
		});
		;
		//mainTable = new WTable(mainCom, SWT.NONE);
		mainTable = new WTable(mainCom, 0, mainSearchText.getVertexY() + 10, mainCom.getSize().x - 10, mainCom.getSize().y - mainSearchText.getVertexY() - 40);
		//mainTable.setSize(1024, 720);
		mainTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int index = mainTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = mainTable.getItem(index);
					if(item != null) {
						String sid = item.getText(0);
						new WDialog(self, new DicEditShell(Integer.valueOf(sid))).open();
						// refresh
						self.listMain(true);
					}
				}
			}
		});
		mainTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.listCol();
			}
		});
		WTableColumn col = new WTableColumn(mainTable, "sID", 100);
		col = new WTableColumn(mainTable, "名称", 100);
		col = new WTableColumn(mainTable, "字段名1", 100);
		col = new WTableColumn(mainTable, "字段名2", 100);
		col = new WTableColumn(mainTable, "字段名3", 100);
		col = new WTableColumn(mainTable, "备注", 100);
		col = new WTableColumn(mainTable, "状态", 60);
		col = new WTableColumn(mainTable, "创建时间", 150);
		col = new WTableColumn(mainTable, "最后修改", 150);
		col = new WTableColumn(mainTable, "版本", 60);
		col = new WTableColumn(mainTable, SWT.NONE);
		
		
		
		
		// item
		colCom = new WComposite(this, mainCom.getVertexX() + 10, mainCom.getLocation().y, mainCom.getSize().x, mainCom.getSize().y);
		btnDetailAdd = new WButton(colCom, colCom.getSize().x - 70, 0, 60, 30, "新增", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = mainTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = mainTable.getItem(index);
					if(item != null) {
						String sid = item.getText(0);
						new WDialog(self, new DicItemAddShell(Integer.valueOf(sid))).open();
						// refresh
						//self.listMain(true);
						self.listCol();
					}
				}				
			}
		});
		colTable = new WTable(colCom, 0, btnDetailAdd.getVertexY() + 10, colCom.getSize().x - 10, colCom.getSize().y - btnDetailAdd.getVertexY() - 30);
		colTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int index = colTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = colTable.getItem(index);
					if(item != null) {
						new WDialog(self, new DicItemEditShell(Integer.valueOf(item.getText(0)), Integer.valueOf(item.getText(1)))).open();
						// refresh
						//self.listMain(true);
						self.listCol();
					}
				}
			}
		});
		col = new WTableColumn(colTable, "mID", 0);
		col = new WTableColumn(colTable, "dID", 60);
		col = new WTableColumn(colTable, "sIndex", 60);
		col.addSelectionListener(new SelectionAdapter() {
			// 排序
		});
		col = new WTableColumn(colTable, "值", 100);
		col = new WTableColumn(colTable, "名称", 150);
		col = new WTableColumn(colTable, "备注", 200);
		col = new WTableColumn(colTable, SWT.NONE);
		
		
		this.listMain(true);
	}
	// 删除
	private void listCol() {
		
		int index = mainTable.getSelectionIndex();
		if(index >= 0) {
			TableItem item = mainTable.getItem(index);
			if(item != null) {
				String sid = item.getText(0);
				colTable.removeAll();
				var items = dda.dicItemService.list(Integer.valueOf(sid));
				if(items != null){
					for(YTDicItem i : items) {
						WTableItem it = new WTableItem(colTable, new String[] {
								String.valueOf(i.getMID()),
								String.valueOf(i.getDID()),
								String.valueOf(i.getSIndex()),
								String.valueOf(i.getXValue()),
								i.getXName(),
								i.getXNote()
						});
					}
				}
			}
			
		}
	}
	protected List<YTDic> dics = null;
	protected void listMain(boolean force) {
		mainTable.removeAll();
		if(dics == null || force) {
			dics = DDA.getInstance().dicService.list();
		}
		if(dics != null) {
			String text = mainSearchText.getText();
			for(YTDic d : dics) {
				
				if(text.length() == 0 || String.valueOf(d.getSID()).contains(text) || d.getXName().contains(text)
						|| d.getXField().contains(text) || d.getXField2().contains(text)
						|| d.getXField3().contains(text) || d.getXNote().contains(text)
						) {
					WTableItem item = new WTableItem(mainTable, new String[] {
							String.valueOf(d.getSID()), d.getXName(), d.getXField(), d.getXField2(), d.getXField3(), d.getXNote(), d.getSStatus().getXName(),
							DateUtil.format(d.getSCreateTime()), DateUtil.format(d.getSLastUpdate()), String.valueOf(d.getSVersion())
					});
				}
			}
		}
		
	}
	
	
	
	
	
	
	
	
	

	/** 暂时还是写死的, 以后要改成从数据库或者配置文件取 2020-02-10 21:33:13 **/
	@Override
	protected String title() {
		return "数据字典";
	}
	
	public static void main(String[] args) {
		new DicShell2().go();
	}

}
